import com.mongodb.*;
//注意本代码可能需要在Eclipise中导入以下驱动
//https://oss.sonatype.org/content/repositories/releases/org/mongodb/mongodb-driver/3.4.2
//mongodb-driver-3.4.2-javadoc.jar
//bson-3.4.2.jar       
//mongo-java-driver-3.4.2.jar
//mongodb-driver-3.4.2-sources.jar
//mongodb-driver-3.4.2.jar  
//mongodb-driver-core-3.4.2.jar

//
//https://oss.sonatype.org/content/repositories/releases/org/mongodb/mongodb-driver-core/3.4.2/mongodb-driver-core-3.4.2.jar
//https://oss.sonatype.org/content/repositories/releases/org/mongodb/bson/3.4.2/
//https://oss.sonatype.org/content/repositories/releases/org/mongodb/mongodb-driver/3.4.2/


//代码参考链接:
//	https://blog.csdn.net/sanpangouba/article/details/78954039

import com.mongodb.*;
 
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Set;
public class Java_mongo {
 
	private static final String MONGO_HOST = "localhost";
	private static final Integer MONGO_PORT = 27017;
	private static final String MONGO_USERNAME = "appleyuchi";
	private static final String MONGO_PASSWORD = "appleyuchi";
	private static final String MONGO_DB_NAME = "school";
	private static final String MONGO_COLLECTION_NAME = "student";
 
 
	public static void main(String[] args) throws UnknownHostException {
 
		// 获取Mongo客户端
		MongoClient mongoClient = new MongoClient(MONGO_HOST, MONGO_PORT);
		/**
		 * 1.获取所有db名称并打印（mongodb未开启auth认证下可用）
		 */
		/*List<String> databaseNames = mongoClient.getDatabaseNames();
		System.out.println(MONGO_HOST + ":" + MONGO_PORT.toString() + "包含如下数据库：");
		for (String databaseName : databaseNames) {
			System.out.println(databaseName);
		}*/
 
		/**
		 * 2.获取到指定db（若不存在，则mongo会创建该db）
		 */
		DB db = mongoClient.getDB(MONGO_DB_NAME);

		
		
		System.out.println(MONGO_DB_NAME + " connection success！");
 
		// 2.2获取该db下所有集合名称并打印
		Set<String> collectionNames = db.getCollectionNames();
		System.out.println(db.getName() + "包含如下集合：");
		for (String collectionName : collectionNames) {
			System.out.println(collectionName);
		}
 
		// 2.3获取指定集合(若不存在，则mongo会创建该集合)
		DBCollection collection = db.getCollection(MONGO_COLLECTION_NAME);
 
		
//--------------------------------------增(完成)-----------------------------------------------------------
//		// 3.1插入一条文档,这里所谓的文档其实就是一条数据
//		BasicDBObject document = new BasicDBObject();
//		document.put("name", "俞驰");
//		document.put("age", 24);
//		document.put("sex", "Beijing");
////		document.put("date", new Date());
//		document.put("class","微电子2班");
//		collection.insert(document);
//		
		
//--------------------------------------查(完成)-------------------------------------------------------	
		
		
// 
//		// 3.2查询一条文档
//		BasicDBObject searchObj = new BasicDBObject();
//		searchObj.put("name", "张三");
//		DBCursor cursor = collection.find(searchObj);
//		if (cursor.hasNext()) {
//			System.out.println("查询到的文档为：");
//			while (cursor.hasNext()) {
//				System.out.println(cursor.next());
//			}
//		} else {
//			System.out.println("该文档不存在！");
//		}
		
		
		
		
//--------------------------------------改(完成)---------------------------------------	
		
		
//		BasicDBObject searchObj   = new BasicDBObject();
//		searchObj.put("name", "张三");             //旧文档-也就是老数据
//		//-------------	
//		BasicDBObject newDocument = new BasicDBObject();
//		newDocument.put("name", "Cheung-updated");// 新文档-也就是新数据
//		//-------------
//		
//		
//		
//		
//		BasicDBObject updateObj = new BasicDBObject();
//		updateObj.put("$set", newDocument);
//		collection.update(searchObj, updateObj);// 更新,相当于用新数据替换了老数据

		
		
//-------------------------------查---------------------------------------	
			
		
// 
		// 3.4查询修改后的文档（修改确认）
		BasicDBObject newDocument = new BasicDBObject();
		newDocument.put("name", "Cheung-updated");// 新文档-也就是新数据
		
		
		DBCursor cursor2 = collection.find(newDocument);
		if (cursor2.hasNext()) {
			System.out.println("修改后的文档为：");
			while (cursor2.hasNext()) {
				System.out.println(cursor2.next());
			}
		} else {
			System.out.println("该文档不存在！");
		}
		

 
	}
 
}
